<?php 
/**
 * @package: Asalsuri
 * @subpackage: Permission Model
 * @author: Truong Kim Phung
 * @date: January 27, 2014
 * 
*/
class Permission_Model extends CI_Model
{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;		
	}
	public function getPermissionByAdmin($intAdminId) {	
	   $stmt = $this->database->prepare('call apermissions_get_admin(?);');
	   $stmt->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
       $stmt->execute();
	   $arrResult = $stmt->fetchAll();	
	   return isset($arrResult) ? $arrResult : null;
	}
	public function getPermission($intAdminId, $intSectionId) {	   
	   $stmt = $this->database->prepare('call apermissions_get(?,?);');
	   $stmt->bindParam(1, $intAdminId,    PDO::PARAM_INT, 11);
	   $stmt->bindParam(2, $intSectionId,  PDO::PARAM_INT, 11);
       $stmt->execute();
	   $arrResult 	= $stmt->fetch();	
	   return isset($arrResult['perm']) ? $arrResult['perm'] : -1;
	}
    public function add($intLoginId, $intAdminId, $intSectionId, $intPerm) {	
		$stmt = $this->database->prepare('call apermissions_add(?,?,?,?)');
		$stmt->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intAdminId, PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intSectionId, PDO::PARAM_INT, 11);
        $stmt->bindParam(4, $intPerm, PDO::PARAM_INT, 6);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();	
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;
		return $intError;
	}
	public function delete($intLoginId, $intAdminId) {		
		$stmt = $this->database->prepare('call apermissions_delete(?,?)');
		$stmt->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intAdminId, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();      
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;			
		return $intError;
	}
	
}